angular.module('wf-utils')
.service('qgyApiService', ($q,$rootScope,urls,$localStorage,wfAlert, $http,userData)->
    ##通过$q,返回promise的形式，调用价格api获得房间价格
    getAsyncHousePrice:(data)->
        def=$q.defer();
        reqData={
            houseId:data.houseId
            lease:data.lease
            payWay:data.payWay
            leaseStart:moment(data.leaseStart).format('YYYY-MM-DD')
        }
        $http.get('/contract/housePrice',params:reqData)
        .success((resp)->
            if resp.code==0
                def.resolve(resp.message)
            else
                def.reject(resp.message)
        )
        return def.promise
    ##通过传递图片input元素的id，对该input绑定图片上传事件，调用上传接口，完成后，广播得到的结果url
    bindImageUploadAndGenApi:(eleId)->
        $(eleId).unbind('change')
        $(eleId).change(()->
            if(!$rootScope.imgFileVd.test($(eleId).eq(0).val()))
                wfAlert.toastInfo('仅接受图片文件')
                return
            formData = new FormData()
            formData.append('file', $(eleId)[0].files[0])
            $http({
                url: urls.backend + urls.imageUploadTo + "/image?token=" + $localStorage['token'],
                method: 'POST'
                headers: {
                    'Content-Type': undefined
                }
                data: formData
                transformRequest: angular.identity
            }).success((resp)->
                if resp.code == 0
                    saveUrl = resp.message.saveUrl
                    url=if saveUrl.indexOf("http") == 0 then saveUrl else urls.backend + '/public/' + saveUrl
                    $rootScope.$broadcast('qgy-image-upload',{eleId:eleId,pictureUrl:url})
            )
        )
    ##通过$q,返回promise的形式，调用user接口，查询是否存在匹配该phone的用户信息
    fetchAsyncUser:(phone,notPerson)->
        def=$q.defer();
        $http.get('/customerUser',params:{page:0,size:5,phone:phone})
        .success((listResp)->
            if listResp.code!=0 || listResp.message.content.length==0
                def.reject(undefined)
            else
                $http.get("/customerUser/#{listResp.message.content[0].id}")
                .success((resp)->
                    if resp.code==0
                        if notPerson
                            def.resolve(resp.message)
                        else
                            personData={
                                customerPhone:resp.message.phone
                                customerName:resp.message.name
                                customerCard:resp.message.idcard
                                idcardFront:resp.message.idcardFront
                                sex:resp.message.sex
                                idcardBack:resp.message.idcardBack
                            }
                            def.resolve(personData)
                    else
                        def.reject(undefined)
                )
                .error((err)->
                    def.reject(undefined)
                )
        ).error((err)->
            def.reject(undefined)
        )
        return def.promise
    ##传入租客信息，住客列表，判断是否需要同步租客信息到住客列表
    syncCotenantByPersonInfo:(data,cotenantList)->
        currentPersonInfo={
            customerName:data.customerName,
            customerCard:data.customerCard
            customerPhone:data.customerPhone
            sex:data.sex
            cardFront:data.idcardFront
            cardBack:data.idcardBack
        }
        if data.customerName and data.customerCard and data.customerPhone
            if cotenantList?.length==0
                return [currentPersonInfo]
            else if cotenantList.length==1
                tmp=_.find(cotenantList,(theItem)->
                    theItem.customerCard==data.customerCard || theItem.customerPhone==data.customerPhone
                )
                return [currentPersonInfo] if tmp
        return cotenantList
)